// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/testing/functions/prestosql/function_base_test.h>

using namespace kumo::pollux;
using namespace kumo::pollux::test;

class IsNullTest : public functions::test::FunctionBaseTest {};

TEST_F(IsNullTest, basic) {
  vector_size_t size = 20;

  // All nulls.
  auto allNulls = make_flat_vector<int32_t>(
      size, [](vector_size_t /*row*/) { return 0; }, null_every(1));
  auto result =
      evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({allNulls}));
  for (int i = 0; i < size; ++i) {
    EXPECT_TRUE(result->value_at(i)) << "at " << i;
  }

  // Nulls in odd positions: 0, null, 2, null,..
  auto oddNulls = make_flat_vector<int32_t>(
      size, [](vector_size_t row) { return row; }, null_every(2, 1));
  result =
      evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({oddNulls}));
  for (int i = 0; i < size; ++i) {
    EXPECT_EQ(result->value_at(i), i % 2 == 1) << "at " << i;
  }

  // No nulls.
  auto noNulls =
      make_flat_vector<int32_t>(size, [](vector_size_t row) { return row; });
  result =
      evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({noNulls}));
  for (int i = 0; i < size; ++i) {
    EXPECT_FALSE(result->value_at(i)) << "at " << i;
  }
}

TEST_F(IsNullTest, constant) {
  // Non-null constant.
  auto data = make_constant<int32_t>(75, 1'000);
  auto result =
      evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({data}));

  assertEqualVectors(make_constant<bool>(false, 1'000), result);

  // Null constant.
  data = make_constant<int32_t>(std::nullopt, 1'000);
  result = evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({data}));

  assertEqualVectors(make_constant<bool>(true, 1'000), result);
}

TEST_F(IsNullTest, dictionary) {
  vector_size_t size = 1'000;

  // Dictionary over flat, no nulls.
  auto flatNoNulls = make_flat_vector<int32_t>({1, 2, 3, 4});
  auto dict = wrap_in_dictionary(
      make_indices(size, [](auto row) { return row % 4; }), size, flatNoNulls);

  auto result =
      evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({dict}));

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary with nulls over no-nulls flat vector.
  dict = BaseVector::wrap_in_dictionary(
      make_nulls(size, null_every(5)),
      make_indices(size, [](auto row) { return row % 4; }),
      size,
      flatNoNulls);

  result = evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({dict}));

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary over flat vector with nulls.
  auto flatWithNulls = make_nullable_flat_vector<int32_t>({1, 2, std::nullopt, 4});
  dict = wrap_in_dictionary(
      make_indices(size, [](auto row) { return row % 4; }), size, flatWithNulls);

  result = evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({dict}));

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary with nulls over flat vector with nulls.
  dict = BaseVector::wrap_in_dictionary(
      make_nulls(size, null_every(5)),
      make_indices(size, [](auto row) { return row % 4; }),
      size,
      flatWithNulls);

  result = evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({dict}));

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), dict->is_null_at(i)) << "at " << i;
  }

  // Dictionary with nulls over constant.
  dict = BaseVector::wrap_in_dictionary(
      make_nulls(size, null_every(5)),
      make_indices(size, [](auto row) { return 2; }),
      size,
      make_constant<int32_t>(75, 10));

  result = evaluate<SimpleVector<bool>>("is_null(c0)", make_row_vector({dict}));

  for (auto i = 0; i < size; ++i) {
    ASSERT_EQ(result->value_at(i), dict->is_null_at(i)) << "at " << i;
  }
}
